1906K - Deck-Building Game - CodeForces Solution


divide and conquer math

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repn(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = (n) - 1; i >= a; i--)
#define pern(i, a, n) for (int i = n; i >= a; i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;

const int N = 1 << 17;
const int M = 1 << 18;
const int mod = 998244353;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;

mt19937_64 Rand((unsigned long long)new char);
#define rand Rand

int n, num[N];
VI g[M][2], t1, t2, t3, t4;
LL pw[M], f[M], ff[M], inv[M];
LL C(int n, int m) {
	LL res = (f[n] * inv[m]) % mod;
	res = (res * inv[n - m]) % mod;
	return res;
}
int pow_mod(int a, LL e) {
	int res = 1;
	for (; e; a = (LL)a * a % mod, e >>= 1) if (e & 1) res = (LL)res * a % mod;
	return res;
}
void fwt_xor(VI &a, int length, int type) {
	int len = -1;
	for (int x = length; x; ++len, x >>= 1);
	repn(i, 1, len) for (int j = 0; j < length; j += 1 << i)
		for (int k = j, szk = 1 << i - 1; k < j + szk; ++k) {
			int s = a[k], t = a[k + szk];
			a[k] = s + t >= mod ? s + t - mod : s + t;
			a[k + szk] = s - t < 0 ? s - t + mod : s - t;
		}
	if (type == 1) return;
	int inv = pow_mod(length, mod - 2);
	rep(i, 0, length) a[i] = 1LL * a[i] * inv % mod;
}
void work(int k, int l, int r) {
	if (l + 1 == r) {
		g[k][0].resize(1), g[k][1].resize(1);
		repn(i, 0, num[l]) {
			int p = i & 1;
			g[k][p][0] = (g[k][p][0] + C(num[l], i) * pw[i]) % mod;
		}
		return;
	}
	int len = (r - l) >> 1;
	work(LC, l, l + len);
	work(RC, l + len, r);
	len <<= 1;
	g[LC][0].resize(len), g[LC][1].resize(len);
	g[RC][0].resize(len), g[RC][1].resize(len);
	rep(i, 0, len / 2) g[RC][1][i + len / 2] = g[RC][1][i], g[RC][1][i] = 0;
	fwt_xor(g[LC][0], len, 1), fwt_xor(g[LC][1], len, 1);
	fwt_xor(g[RC][0], len, 1), fwt_xor(g[RC][1], len, 1);
	t1.clear(), t1.resize(len);
	t2.clear(), t2.resize(len);
	t3.clear(), t3.resize(len);
	t4.clear(), t4.resize(len);
	rep(i, 0, len) {
		t1[i] = (LL)g[LC][0][i] * g[RC][0][i] % mod;
		t2[i] = (LL)g[LC][0][i] * g[RC][1][i] % mod;
		t3[i] = (LL)g[LC][1][i] * g[RC][0][i] % mod;
		t4[i] = (LL)g[LC][1][i] * g[RC][1][i] % mod;
	}
	fwt_xor(t1, len, -1), fwt_xor(t2, len, -1), fwt_xor(t3, len, -1), fwt_xor(t4, len, -1);
	g[k][0].resize(len), g[k][1].resize(len);
	if (len == r) {
		rep(i, 0, len) g[k][0][i] = ((LL)t1[i] + t2[i] + t3[i] + t4[i]) % mod;
	}
	else {
		rep(i, 0, len) {
			g[k][0][i] = (t1[i] + t4[i]) % mod;
			g[k][1][i] = (t2[i] + t3[i]) % mod;
		}
	}
}
int main() {
	IO;
	pw[0] = 1;
	rep(i, 1, M) pw[i] = pw[i - 1] * 2 % mod;
	f[0] = 1;
	rep(i, 1, M) f[i] = (f[i - 1] * i) % mod;
	ff[1] = ff[0] = inv[1] = inv[0] = 1;  
	rep(i, 2, M) {
    	inv[i] = (LL)(mod - mod / i) * inv[mod % i] % mod;
    	ff[i] = inv[i];
	}
	rep(i, 2, M) inv[i] = (inv[i - 1] * inv[i]) % mod;
	cin >> n;
	repn(i, 1, n) {
		int x;
		cin >> x;
		num[x]++;
	}
	work(1, 0, N);
	cout << g[1][0][0] << "\n";
	return 0;
}


Comments

Submit
0 Comments
More Questions

1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies
1299A - Anu Has a Function
1111A - Superhero Transformation
954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction